//
//  YJStatusRetweetView.swift
//  YJWeiBo
//
//  Created by Jim1024 on 2017/1/18.
//  Copyright © 2017年 Jim1024. All rights reserved.
//

import UIKit
import SnapKit

/// 被转发微博的view
class YJStatusRetweetView: UIView {

    // MARK: - 属性
    var retweetViewBottomCons: Constraint?
    
    /// 被转发的微博模型
    var retweetStatus: YJStatus? {
        didSet {
            // 当别人给被转发的模型赋值后,将模型的内容赋给对应的view
            // @发微博人的名字: 微博内容
            guard let screen_name = retweetStatus?.user?.screen_name else {
                JPrint("retweetStatus?.user?.screen_name 没有值")
                return
            }
            
            guard let text = retweetStatus?.text else {
                JPrint("retweetStatus?.text没有值")
                return
            }
            
            var content = "@" + screen_name
            content.append(text)
            retweetContentLabel.text = content
            
            // 判断被转发的微博里面是否有配图
            self.retweetViewBottomCons?.deactivate()
            if let pictureURLs = retweetStatus?.storePictureURLs {
                // 有图片要显示,显示retweetPictureView
                retweetPictureView.isHidden = false
                
                // 将被转发微博的图片URL赋值给retweetPictureView
                retweetPictureView.pictureURLs = pictureURLs
                
                self.snp.makeConstraints({ (make) in
                    self.retweetViewBottomCons = make.bottom.equalTo(retweetPictureView).offset(YJStatusCellMargin).constraint
                })
            } else {
                // 没有图片要显示
                retweetPictureView.isHidden = true
                self.snp.makeConstraints({ (make) in
                    self.retweetViewBottomCons = make.bottom.equalTo(retweetContentLabel).offset(YJStatusCellMargin).constraint
                })
            }
            setNeedsLayout()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initCutomView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func initCutomView() {
        addSubview(bgButton)
        addSubview(retweetContentLabel)
        addSubview(retweetPictureView)
        
        /*================= layout =================*/
        
        bgButton.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        retweetContentLabel.snp.makeConstraints { (make) in
            make.top.leading.equalToSuperview().offset(YJStatusCellMargin)
            make.trailing.equalToSuperview().offset(-YJStatusCellMargin)
        }
        
        retweetPictureView.snp.makeConstraints { (make) in
            make.leading.equalTo(retweetContentLabel)
            make.top.equalTo(retweetContentLabel.snp.bottom).offset(YJStatusCellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        self.snp.makeConstraints { (make) in
            self.retweetViewBottomCons = make.bottom.equalTo(retweetPictureView).offset(YJStatusCellMargin).constraint
        }
    }
    
    // MARK: - 懒加载
    
    fileprivate lazy var bgButton: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "retweet_background"), for: UIControlState())
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), for: UIControlState.highlighted)
        // MARK: - test...
        button.backgroundColor = UIColor.red
        
        return button
    }()
    
    /// 被转发微博内容label
    fileprivate lazy var retweetContentLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: YJStatusCellContentSize)
        label.textColor = UIColor.darkGray
        label.numberOfLines = 0
        
        // 换行/折行宽度
        label.preferredMaxLayoutWidth = UIScreen.width - 2 * YJStatusCellMargin
        
        return label
    }()
    
    /// 微博配图
    fileprivate lazy var retweetPictureView: YJStatusPictureView = YJStatusPictureView()

}
